<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item label="员工姓名" prop="empName"  >
        <el-input
          style="width: 160px;"
          v-model="queryParams.empName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请假类型" prop="abStyel">
<!--        <el-input-->
<!--          v-model="queryParams.abStyel"-->
<!--          placeholder="请输入请假类型"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--        请假类型输入框改下拉框-->
        <el-select v-model="queryParams.abStyel" placeholder="请选择" clearable style="width: 160px;">
          <el-option

            v-for="item in vacate"
            :key="item.id"
            :label="item.style"
            :value="item.style">
          </el-option>
        </el-select>

<!--        <el-select v-model="form.empId" placeholder="请选择">-->
<!--          <el-option-->
<!--            v-for="item in employee"-->
<!--            :key="item.empId"-->
<!--            :label="item.empName"-->
<!--            :value="item.empId">-->
<!--          </el-option>-->
<!--        </el-select>-->

      </el-form-item>
      <el-form-item label="起始时间" prop="abStartTime" >
        <el-date-picker clearable
                        style="width: 160px;"
                        v-model="queryParams.abStartTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择起始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="abEndTime">
        <el-date-picker clearable
                        style="width: 160px;"
                        v-model="queryParams.abEndTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="请假原因" prop="abCause">-->
<!--        <el-input-->
<!--          v-model="queryParams.abCause"-->
<!--          placeholder="请输入请假原因"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['check:vacate:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          round
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['check:vacate:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['check:vacate:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          round
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['check:vacate:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vacateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="记录号" align="center" prop="abId"/>
      <el-table-column label="员工编号" align="center" prop="empId"/>
      <el-table-column label="员工姓名" align="center" prop="empName"/>
      <el-table-column label="请假类型" align="center" prop="abStyel"/>
      <el-table-column label="请假原因" align="center" prop="abCause"/>
      <el-table-column label="起始时间" align="center" prop="abStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.abStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="abEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.abEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      审核状态字段-->
      <el-table-column label="审核状态" align="center" prop="state">
        <!--        自定义标签-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 0">未审核</el-tag>
          <el-tag type="warning" v-if="scope.row.state === 1">审核中</el-tag>
          <el-tag type="success" v-if="scope.row.state === 2">审核通过</el-tag>
          <el-tag type="danger" v-if="scope.row.state === 3">审核未通过</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <!--          审核状态_头-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleCheck(scope.row)"
            v-hasPermi="['check:vacate:state']"
          >审核
          </el-button>
          <!--          审核状态_尾-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['check:vacate:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['check:vacate:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--    审核对话框-->
    <el-dialog :title="title" :visible.sync="stateOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="abCause">
          <!--          下拉框-->
          <!--          <el-input v-model="form.empId" placeholder="请输入员工编号" />-->
          <el-select v-model="form.empId" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="申请类别" prop="applyforType">
          <!--          申请类别单选框-->
          <!--          <el-input v-model="form.applyforType" placeholder="请输入申请类别" />-->
          <template>
            <el-radio v-model="form.applyforType" label="请假申请">请假申请</el-radio>
            <el-radio v-model="form.applyforType" label="其他申请">其他申请</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="审批内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入审批内容" :autosize="{ minRows: 4}"  maxlength="100" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStateForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改请假管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="empId">
          <!--          下拉框-->
          <!--          <el-input v-model="form.empId" placeholder="请输入员工编号" />-->
          <el-select v-model="form.empId" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请假类型" prop="abStyel">
          <!--          <el-input v-model="form.abStyel" placeholder="请输入请假类型" />-->
          <!--          单选框-->
          <template>
            <el-radio v-model="form.abStyel" label="病假">病假</el-radio>
            <el-radio v-model="form.abStyel" label="公假">公假</el-radio>
            <el-radio v-model="form.abStyel" label="事假">事假</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="起始时间" prop="abStartTime">
          <el-date-picker clearable
                          v-model="form.abStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="abEndTime">
          <el-date-picker clearable
                          v-model="form.abEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请假原因" prop="abCause">
          <el-input v-model="form.abCause" type="textarea" placeholder="请输入请假原因" :autosize="{ minRows: 4}"
                    maxlength="30" show-word-limit/>
        </el-form-item>
        <!--        <el-form-item label="版本号" prop="version">-->
        <!--          <el-input v-model="form.version" placeholder="请输入版本号" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listVacate, getVacate, delVacate, addVacate, updateVacate, listEmployee} from "@/api/check/vacate";
//调取新增审核接口
import {addApprove} from "@/api/audit/approve";

export default {
  name: "Vacate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示审核弹出层
      stateOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        abStyel: null,
        abStartTime: null,
        abEndTime: null,
        abCause: null,
        empName:null,
      },
      //下拉框数据
      employee: [],
      //请假数据的下拉框
      vacate:[{id:1,style:"病假"},{id:2,style:"公假"},{id:3,style:"事假"}],
      // 请假管理表格数据
      vacateList: [],
      // 表单参数
      form: {},
      // 备注信息
      remark: '',
      // 表单校验
      rules: {
        empId: [
          {required: true, message: "员工姓名不能为空", trigger: "blur"}
        ],
        abStyel: [
          {required: true, message: "请假类型不能为空", trigger: "blur"}
        ],
        abStartTime: [
          {required: true, message: "起始时间不能为空", trigger: "blur"}
        ],
        abEndTime: [
          {required: true, message: "结束时间不能为空", trigger: "blur"}
        ],
        abCause: [
          {required: true, message: "请假原因不能为空", trigger: "blur"},
          {max: 29, message: "最大字符应小于30个", trigger: "change"}
        ],
        //审批校验
        applyforType: [
          {required: true, message: "申请类别不能为空", trigger: "blur"},
        ],
        content: [
          {required: true, message: "审批内容不能为空", trigger: "blur"},
          {max: 99, message: "最大字符应小于100个", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请假管理列表 */
    getList() {
      this.loading = true;
      listVacate(this.queryParams).then(response => {
        this.vacateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.stateOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        abId: null,
        empId: null,
        abStyel: null,
        abStartTime: null,
        abEndTime: null,
        abCause: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.abId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假管理";

      // 发送请求
      listEmployee().then(response => {
        this.employee = response.rows;
        this.loading = false;
      });

    },
    /** 提交审核按钮操作 */
    handleCheck(row) {
      if (row.state == 1) {
        this.$modal.confirm('已在审核中')
      } else if (row.state == 2) {
        this.$modal.confirm('审核已通过')
      } else {
        //表单重置
        this.reset();
        //修改弹窗
        //this.stateOpen = true;
        //this.title = "添加申请管理";
        const abId = row.abId || this.ids
        getVacate(abId).then(response => {
          this.remark = 'vacate/' + abId
          this.form = response.data;
          this.stateOpen = true;
          this.title = "添加审核管理";
        });
      }


    },

    /** 提交审核数据 */
    submitStateForm() {
      //addApprove
      this.form.remark = this.remark;
      this.form.state = 1;
      //数据校验
      this.$refs["form"].validate(valid => {
        if (valid) {
          //验证数据不为空操作

          addApprove(this.form).then(response => {
            this.$modal.msgSuccess("新增审核成功");
            //this.stateOpen = false;
            //this.getList();
          }),
            //
            updateVacate(this.form).then(response => {
              //this.$modal.msgSuccess("修改成功");
              this.stateOpen = false;
              this.getList();
            })
        }
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const abId = row.abId || this.ids
      getVacate(abId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改请假管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.abId != null) {
            updateVacate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVacate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const abIds = row.abId || this.ids;
      this.$modal.confirm('是否确认删除请假管理编号为"' + abIds + '"的数据项？').then(function () {
        //let axiosPromise = delVacate(abIds);
        //是否删除逻辑
        //return delVacate(abIds);
      }).then(() => {
        let res = delVacate(abIds);
        res.then((res) => {
          if (res) {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          } else {
            this.$modal.msgError("未达到结束时间，删除失败");
          }
        })

      }).catch(() => {

      });
    },
    // handleDelete(row) {
    //   const abIds = row.abId || this.ids;
    //   this.$modal.confirm('是否确认删除请假管理编号为"' + abIds + '"的数据项？').then(function() {
    //     let axiosPromise = delVacate(abIds);
    //     //是否删除逻辑
    //     return delVacate(abIds);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },

    /** 导出按钮操作 */
    handleExport() {
      this.download('check/vacate/export', {
        ...this.queryParams
      }, `vacate_${new Date().getTime()}.xlsx`)
    },

  },
  mounted() {
    // 发送查询所有员工的请求
    listEmployee().then(response => {
      this.employee = response.rows;
      this.loading = false;
      console.log("员工数据"+employee);
    });
  }
};
</script>
